package warehouse;

import entity.CommentsLocalHome;
import entity.OrdersLocal;
import entity.OrdersLocalHome;
import entity.UsersLocalHome;
import java.io.IOException;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import transfer.Comment;
import transfer.Order;
import transfer.OrderState;
import transfer.User;

/**
 *
 * @author Sergey, Oleh
 */
public class OrderOperation extends HttpServlet {

    public static final String shipAction = "ship";
    public static final String cancelAction = "cancel";
    public static final String actionParameter = "action";
    public static final String idParameter = "id";
    public static final String commentParameter = "comment";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer idOrder = Integer.valueOf(request.getParameter(idParameter));
        String action = request.getParameter(actionParameter);
        String comment = request.getParameter(commentParameter);

        System.out.println("action = " + action);
        System.out.println("id = " + idOrder);
        System.out.println("comment = " + comment);

        try {
            InitialContext context = new InitialContext();
            OrdersLocalHome homeOrder = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            OrdersLocal localOrder = homeOrder.findByPrimaryKey(idOrder);
            Order order = localOrder.getOrder();
            if (order.getOrderState().equals(OrderState.QUEUED)) {
                if (shipAction.equals(action)) {
                    UsersLocalHome homeUser = (UsersLocalHome) context.lookup("java:comp/env/Users");
                    User courier = homeUser.findNextCourier().getUser();
                    System.out.println("idCourier = " + courier.getIdUser());
                    order.setCourier(courier);
                    order.setOrderState(OrderState.SHIPPING);
                }
                if (cancelAction.equals(action)) {
                    order.setOrderState(OrderState.CANCELED);
                }
                localOrder.setOrder(order);
                if (comment != null && !comment.equals("")) {
                    Comment newComment = new Comment();
                    newComment.setUser(order.getWarehouseEmployee());
                    newComment.setOrder(order);
                    newComment.setOrderState(order.getOrderState());
                    newComment.setCommentText(comment);
                    newComment.setCommentDate(new Date());
                    CommentsLocalHome commentsHome = (CommentsLocalHome) context.lookup("java:comp/env/Comments");
                    commentsHome.create(newComment);
                }
            }
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        } catch (CreateException ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
